﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace XMLConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = "cdcs\\2A353.txt";

            StringBuilder sb = new StringBuilder();

            Console.WriteLine("Converting " + file + " to XML....");

            using (StreamReader sr = new StreamReader(file))
            {
                sb.Append("<questions>\n");

                string line = "";

                int questionNum = 1;
                int lineNum = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    lineNum++;

                    if (line.Trim() == "" || line.StartsWith("[")) continue;
                    
                    string[] items = line.Split(':');
                    
                    if (items.Length == 2)
                    {
                        sb.Append("\t<q" + (questionNum) + ">\n");
                        sb.Append("\t\t<question>" + items[0] + "?</question>\n");
                        sb.Append("\t\t<choice1>" + items[1] + ".</choice1>\n");

                        for (int i = 2; i < 5; i++)
                        {
                            sb.Append("\t\t<choice" + i + ">None.</choice" + i + ">\n");
                        }
                        sb.Append("\t\t<answer>1</answer>\n");
                        sb.Append("\t</q" + (questionNum++) + ">\n");
                    }
                    else if (items.Length == 6)
                    {
                        sb.Append("\t<q" + (questionNum) + ">\n");
                        sb.Append("\t\t<question>" + items[0] + "?</question>\n");

                        for (int i = 1; i < 5; i++)
                        {
                            sb.Append("\t\t<choice" + i + ">" + items[i] + "</choice" + i + ">\n");
                        }
                        sb.Append("\t\t<answer>" + items[5] + "</answer>\n");
                        sb.Append("\t</q" + (questionNum++) + ">\n");
                    }
                    else
                    {
                        Console.Out.WriteLine("Line " + lineNum + " doesnt have enough parameters");
                    }
                }

                sb.Append("</questions>\n");

                using (StreamWriter sw = new StreamWriter("cdcs\\2A353.xml"))
                {
                    sw.WriteLine(sb.ToString());
                }

                Console.WriteLine("Conversion done\nPress any key to continue...");
                Console.Read();

            }
        }
    }
}
